<template>
  <div>
    <el-form ref="form" :model="hospitalSet" :rules="rules" label-width="100px" style="text-align: center">
      <el-form-item label="医院名称" prop="hosname">
        <el-input v-model="hospitalSet.hosname" />
      </el-form-item>
      <el-form-item label="医院编号" prop="hoscode">
        <el-input v-model="hospitalSet.hoscode" />
      </el-form-item>
      <el-form-item label="api基础路径" prop="apiUrl">
        <el-input v-model="hospitalSet.apiUrl" />
      </el-form-item>
      <el-form-item label="联系人姓名" prop="contactsName">
        <el-input v-model="hospitalSet.contactsName" />
      </el-form-item>
      <el-form-item label="联系人手机" prop="contactsPhone">
        <el-input v-model="hospitalSet.contactsPhone" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveOrUpdate">保存</el-button>
        <el-button type="danger" @click="ResetTable">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import hospSet from '@/api/hosp/hospSet'
export default {
  name: 'Form',
  data() {
    return {
      hospitalSet: {},
      rules: {
        hosname: [{ required: true, message: '请输入医院名称', trigger: 'blur' }],
        hoscode: [{ required: true, message: '请输入医院编号', trigger: 'blur' }],
        apiUrl: [{ required: true, message: '请输入api基础路径', trigger: 'blur' }],
        contactsName: [{ required: true, message: '请输入联系人姓名', trigger: 'blur' }],
        contactsPhone: [{ required: true, message: '请输入联系人手机号', trigger: 'blur' }]
      }
    }
  },
  created() {
    if (this.$route.params && this.$route.params.id) {
      const id = this.$route.params.id
      this.editHospSet(id)
    }
  },
  methods: {
    editHospSet(id) {
      hospSet.getHospSet(id).then((res) => {
        this.hospitalSet = res.data
      }).catch((error) => {
        console.log(error)
      })
    },
    edit() {
      hospSet.updateHospSet(this.hospitalSet).then((res) => {
        this.$message({
          type: 'success',
          message: '编辑成功!'
        })
        this.$router.push('/hospSet/list')
      }).catch((error) => {
        console.log(error)
      })
    },
    save() {
      hospSet.addHospSet(this.hospitalSet).then((res) => {
        this.$message({
          type: 'success',
          message: '添加成功!'
        })
        this.$router.push('/hospSet/list')
      }).catch((error) => {
        console.log(error)
      })
    },
    saveOrUpdate() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          !this.hospitalSet.id ? this.save() : this.edit()
        } else {
          this.$message({
            type: 'warning',
            message: '请输入内容'
          })
          return
        }
      })
    },
    ResetTable() {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="less">
</style>
